package com.test.controller.hr;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.ResData;
import com.test.entity.*;
import com.test.service.AttendLogService;
import com.test.service.HrInfoService;
import com.test.service.LeaveInfoService;
import com.test.service.StaffInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@Controller
public class HrController {

    @Autowired
    private HrInfoService hrInfoService;
    @Autowired
    private AttendLogService attendLogService;
    @Autowired
    private StaffInfoService staffInfoService;
    @Autowired
    private LeaveInfoService leaveInfoService;

//    进入个人信息页面
    @GetMapping("hr/index")
    public String getCw(){
        return "hr/index";
    }
//    显示个人信息
    @GetMapping("hr/hr_info/detail")
    public ModelAndView getHrInfoDetail(HttpSession session){
        Map login =(Map)session.getAttribute("login");
        Integer id =(Integer)login.get("id");
        HrInfo hrInfo = hrInfoService.queryById(id);
        ModelAndView modelAndView = new ModelAndView("hr/personal");
        modelAndView.addObject("detail",hrInfo);
        return modelAndView;
    }

//    显示考勤页面
    @GetMapping("hr/attend_log")
    public String getAttendLog(){
        return "hr/attend_log/list";
    }

    //    显示所有的考勤信息
    @GetMapping("hr/attend_log/queryList")
    @ResponseBody
    public ResData  getQueryList(Integer page, AttendLog attendLog, HttpServletResponse response) throws JsonProcessingException {
        return attendLogService.queryByPage(attendLog, PageRequest.of(page-1,10));
    }
    //    跳转新增考勤信息
    @GetMapping("hr/attend_log/add")
    public ModelAndView getAdd(){
        ModelAndView modelAndView = new ModelAndView("hr/attend_log/add_page");
        modelAndView.addObject("staffInfoList",staffInfoService.selectAll());//所有员工信息
        return modelAndView;
    }
    //    提交新增考勤信息
    @PostMapping("hr/attend_log/add_submit")
    @ResponseBody
    public ResData postAttendLogAddSubmit(Integer staffId,AttendLog attendLog){

        StaffInfo staffInfo = staffInfoService.queryById(staffId);

        System.out.println(staffId+"   "+attendLog.toString());
        System.out.println(staffInfo.toString());

        attendLog.setStaffNo(staffInfo.getStaffNo());
        attendLog.setStaffName(staffInfo.getRealName());
        attendLog.setCreateTime(new Date());

        Integer insert = attendLogService.insert(attendLog);

        ResData resData = new ResData();
        if (insert>0){
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }
    //    跳转修改考勤信息
    @GetMapping("hr/attend_log/update")
    public ModelAndView getUpdate(Integer id){
        ModelAndView modelAndView  = new ModelAndView("hr/attend_log/update_page");
        AttendLog attendLog = attendLogService.queryById(id);
        modelAndView.addObject("data",attendLog);
        modelAndView.addObject("staffInfoList",staffInfoService.selectAll());//所有员工信息
        return modelAndView;
    }
    //    提交修改考勤信息
    @PostMapping("hr/attend_log/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(Integer id,Integer staffId ,AttendLog attendLog){

        StaffInfo staffInfo = staffInfoService.queryById(staffId);

        attendLog.setId(id);

        attendLog.setStaffNo(staffInfo.getStaffNo());
        attendLog.setStaffName(staffInfo.getRealName());
        attendLog.setCreateTime(new Date());

        ResData resData = new ResData();

        AttendLog attendLog1 = attendLogService.update(attendLog);
        if (attendLog1.getId()!=null ||!attendLog1.getId().equals("")){
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("修改失败");
        return resData;
    }
    //    删除考勤信息
    @GetMapping("hr/attend_log/del2")
    @ResponseBody
    public ResData getDelete(Integer id){
        attendLogService.deleteById(id);
        ResData resData = new ResData();
        resData.setCode(1);
        resData.setMsg("删除成功");
        return resData;
    }

//    请假管理
    @GetMapping("hr/leave_info")
    public String getHrLeaveInfo(){
        return "hr/leave_info/list";
    }
//    显示请假信息
    @GetMapping("hr/leave_info/queryList")
    @ResponseBody
    public ResData getLeaveInfoQueryList(Integer page, LeaveInfo leaveInfo){
        return leaveInfoService.queryByPage(leaveInfo,PageRequest.of(page-1,10));
    }
//    提交同意请假信息
    @GetMapping("hr/leave_info/ty")
    @ResponseBody
    public ResData getLeaveInfoTy(Integer id){
        ResData resData = new ResData();
        LeaveInfo leaveInfo = leaveInfoService.queryById(id);
        leaveInfo.setLeaveStatus(2);
        int update = leaveInfoService.update(leaveInfo);
        if (update>0){
            resData.setCode(1);
            resData.setMsg("已同意");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("失败");
        return resData;

    }

//    跳转驳回请假信息页面
    @GetMapping("hr/leave_info/bh")
    public ModelAndView getLeaveInfoBh(Integer id){
        ModelAndView modelAndView = new ModelAndView("hr/leave_info/bh_page");
        modelAndView.addObject("data",leaveInfoService.queryById(id));
        return modelAndView;

    }
//    提交驳回请假信息
    @PostMapping("hr/leave_info/bh_submit")
    @ResponseBody
    public ResData postLeaveInfoBhSubmit(Integer id,String checkRemark){
        ResData resData = new ResData();
        LeaveInfo leaveInfo = leaveInfoService.queryById(id);
        leaveInfo.setLeaveStatus(3);
        leaveInfo.setCheckRemark(checkRemark);
        int update = leaveInfoService.update(leaveInfo);
        if (update>0){
            resData.setCode(200);
            resData.setMsg("驳回成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("驳回失败");
        return resData;
    }

}
